Skip to content

Fix MA0182 to handle COM interop types with CoClass attribute#959

Merged
meziantou merged 5 commits into
mainfrom
copilot/handle-com-objects
Jan 10, 2026
Merged

Fix MA0182 to handle COM interop types with CoClass attribute#959
meziantou merged 5 commits into
mainfrom
copilot/handle-com-objects

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 10, 2026

MA0182 incorrectly reports internal COM interop types as unused when they use the [CoClass] attribute for runtime binding, making their relationship invisible to static analysis.

[ComImport]
[CoClass(typeof(FileSaveDialogRCW))]
internal interface NativeFileSaveDialog { } // Was incorrectly flagged as unused

[ComImport]
internal sealed class FileSaveDialogRCW { } // Was incorrectly flagged as unused

Changes

  • Track [CoClass] relationships: When an interface has [CoClass(typeof(T))], mark both the interface and T as used since they form a COM interop pair
  • Add test coverage: Validates both interface and CoClass implementation are not reported

The analyzer now properly understands COM interop patterns where an interface and its CoClass implementation work together as a unit, even when there are no explicit references to them in code.

[!WARNING]

Original prompt

This section details on the original issue you should resolve

<filter_complete></filter_complete>

<issue_title>MA0184 should handle COM objects</issue_title>
<issue_description>````c#
[ComImport]
[Guid(IIDGuid.IFileSaveDialog)]
[CoClass(typeof(FileSaveDialogRCW))]
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Used as a class")]
internal interface NativeFileSaveDialog : IFileSaveDialog // This class should not be reported because of CoClass. There is a usage through FileSaveDialogRCW
{
}

[ComImport]
[ClassInterface(ClassInterfaceType.None)]
[Guid(CLSIDGuid.FileSaveDialog)]
internal sealed class FileSaveDialogRCW
{
}


## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>
Original prompt

This section details on the original issue you should resolve

<issue_title>MA0184 should handle COM objects</issue_title>
<issue_description>````c#
[ComImport]
[Guid(IIDGuid.IFileSaveDialog)]
[CoClass(typeof(FileSaveDialogRCW))]
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Used as a class")]
internal interface NativeFileSaveDialog : IFileSaveDialog // This class should not be reported because of CoClass. There is a usage through FileSaveDialogRCW
{
}

[ComImport]
[ClassInterface(ClassInterfaceType.None)]
[Guid(CLSIDGuid.FileSaveDialog)]
internal sealed class FileSaveDialogRCW
{
}


## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ass relationships

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MA0184 to handle COM objects Fix MA0182 to handle COM interop types Jan 10, 2026
Copilot AI requested a review from meziantou January 10, 2026 03:22
Comment on lines +83 to +85
// Exclude COM types (types marked with ComImportAttribute)
if (symbol.HasAttribute(ComImportAttributeSymbol))
return;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot I'm not sure why [ComImport] types should be excluded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - there's no need to exclude all [ComImport] types. The CoClass tracking is sufficient. I've removed the blanket exclusion and updated the logic to mark both the interface and its CoClass implementation as used when the CoClass attribute is present. This correctly handles the COM interop pair. (commit a5309ff)

…s used

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 10, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.nuget.org
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/Meziantou.Analyzer.Test.runtimeconfig.json --depsfile /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/Meziantou.Analyzer.Test.deps.json /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/testhost.dll --port 44679 --endpoint 127.0.0.1:044679 --role client --parentprocessid 4352 --datacollectionport 35623 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Fix MA0182 to handle COM interop types Fix MA0182 to handle COM interop types with CoClass attribute Jan 10, 2026
Copilot AI requested a review from meziantou January 10, 2026 04:25
@meziantou meziantou marked this pull request as ready for review January 10, 2026 04:31
This was referenced May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MA0182 should handle COM objects

2 participants